from bs4 import BeautifulSoup

from urllib.request import urlopen, Request

import json

code = ['601318', '000333']

address = 'http://quotes.money.163.com'

table_list = ['zcfzb', 'lrb', 'xjllb']

for i in code:

    for j in table_list:
        url = 'http://quotes.money.163.com/f10/%s_%s.html' % (j, i)

        print(url)

        header = {

            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',

            'Host': 'quotes.money.163.com',

            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) '

                          'Version/14.1.2 Safari/605.1.15',

            'Accept-Language': 'zh-cn',

            'Connection': 'keep-alive'

        }

        u = Request(url, headers=header)

        html = urlopen(u)

        soup = BeautifulSoup(html, 'lxml')

        div = soup.find_all('a', class_='download_link')

        nurl = address + div[0].get('href')

        nu = Request(nurl, headers=header)

        nhtml = urlopen(nu)

        bs = BeautifulSoup(nhtml, 'lxml')

        txt = bs.text.replace('--', '0')

        excel = open('F:/PythonProjects/First/excel/%s%s.csv' % (i, j), 'w', encoding='utf_8_sig').write(txt)